package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.service.AutoCleanService;
import com.keldian.mobilesafe.utils.ServiceUtils;

/**
 * Created by Administrator on 16-1-18.
 */
public class TaskSettingActivity extends Activity {
    private CheckBox cb_show_system;
    private CheckBox cb_auto_clean;
    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_setting);

        sp = getSharedPreferences("config", MODE_PRIVATE);
        cb_show_system = (CheckBox) findViewById(R.id.cb_show_system);
        cb_auto_clean = (CheckBox) findViewById(R.id.cb_auto_clean);
        cb_show_system.setChecked(sp.getBoolean("showsystem", false));
        cb_auto_clean.setChecked(sp.getBoolean("autoclean", false));
        cb_show_system.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("showsystem", isChecked);
                editor.commit();
            }
        });


//        CountDownTimer cdt = new CountDownTimer(5000, 1000) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//                ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//                am.killBackgroundProcesses("com.android.inputmethod.latin");
//                Log.d("TaskSettingActivity", "com.android.inputmethod.latin killed!!!");
//            }
//
//            @Override
//            public void onFinish() {
//                System.out.printf("finish");
//            }
//        };
//        cdt.start();

        cb_auto_clean.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("autoclean", isChecked);
                editor.commit();
                Intent intent = new Intent(TaskSettingActivity.this, AutoCleanService.class);
                if (isChecked) {
                    startService(intent);
                } else {
                    stopService(intent);
                }
            }
        });

    }

    @Override
    protected void onStart() {
        boolean running = ServiceUtils.isServiceRunning(this, "com.keldian.mobilesafe.service.AutoCleanService");
        cb_auto_clean.setChecked(running);
        super.onStart();
    }
}
